package com.ys.controller;


import com.ys.pojo.Company;
import com.ys.pojo.Drugs;
import com.ys.service.CompanyService;
import com.ys.service.DrugsService;
import com.ys.vo.ResultVo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/zdrug")
public class ZDrugController {

    /**
     * 单位/瓶、盒....
     */
    @Resource
    private CompanyService companyService;
    /**
     * 药品
     */
    @Resource
    private DrugsService drugsService;

    @GetMapping("/company")
    public ResultVo company(){
        List<Company> list = companyService.select();
        return ResultVo.success(list);
    }

    @PostMapping(value = "/update", produces = "application/json;charset=utf-8")
    public ResultVo update(@RequestBody Drugs drugs){
        boolean isResult = drugsService.update(drugs);
        if (!isResult){
            return ResultVo.failure("修改失败....");
        }
        return ResultVo.success("修改成功!");
    }
    @PostMapping(value = "/add", produces = "application/json;charset=utf-8")
    public ResultVo add(@RequestBody Drugs drugs){
        boolean isResult = drugsService.add(drugs);
        if (!isResult){
            return ResultVo.failure("添加失败....");
        }
        return ResultVo.success("添加成功!");
    }

    @PostMapping(value = "/del", produces = "application/json;charset=utf-8")
    public ResultVo del(Integer id){
        boolean isResult = drugsService.del(id);
        if (!isResult){
            return ResultVo.failure("删除失败....");
        }
        return ResultVo.success("删除成功!");
    }

    /**
     * 查询全部药品
     * @return  返回药品集合
     */
    @GetMapping("/searchAll")
    public ResultVo searchAll(){
        List<Drugs> list = drugsService.searchAll();
        System.out.println(list);
        return ResultVo.success(list);
    }

}
